package com.mccken.designpattern.proxy.proxy2;

import com.mccken.designpattern.proxy.support.MetricsCollector;
import com.mccken.designpattern.proxy.support.RequestInfo;
import com.mccken.designpattern.proxy.support.UserVo;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2022-10-26 19:51
 **/
public class UserControllerProxy implements IUserController {
	
	private MetricsCollector metricsCollector;
	private UserController userController;
	
	public UserControllerProxy(UserController userController) {
		this.metricsCollector = new MetricsCollector();
		this.userController = userController; 
	}
	
	@Override
	public UserVo login(String telephone, String password) {
		long startTimeStamp = System.currentTimeMillis();
		UserVo userVo = userController.login(telephone, password);
		long endTimeStamp =  System.currentTimeMillis();
		long responseTime = endTimeStamp - startTimeStamp;
		RequestInfo login = new RequestInfo("login", responseTime, startTimeStamp);
		metricsCollector.recordRequest(login);
		return userVo;
	}

	@Override
	public UserVo register(String telephone, String password) {
		long startTimeStamp = System.currentTimeMillis();
		
		UserVo userVo = userController.register(telephone, password);
		
		long endTimeStamp =  System.currentTimeMillis();
		long responseTime = endTimeStamp - startTimeStamp;
		RequestInfo login = new RequestInfo("register", responseTime, startTimeStamp);
		metricsCollector.recordRequest(login);
		return userVo;
	}
}
